package com.qiaoyanqing.ximalaya.discovermodule.discover_fragment;


import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverRankActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverRankAnchorActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverRankSecondSingleActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverWebActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.DiscoverRankAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverRankFirstService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankDataEntity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankEntity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankFocusEnity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankListEntity;
import com.qiaoyanqing.ximalaya.url.Urlconfig;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverRankFragment extends Fragment implements AdapterView.OnItemClickListener, Callback<DiscoverRankEntity>, View.OnClickListener {

    private DiscoverRankAdapter programAdapter;
    private DiscoverRankAdapter anchorAdapter;
    private List<DiscoverRankListEntity>programlist;
    private List<DiscoverRankListEntity>anchorlist;
    private ListView programListView;
    private ListView anchorListView;
    private ImageView focusImage;
    private DiscoverRankFocusEnity focusEnity;
    public DiscoverRankFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView=inflater.inflate(R.layout.fragment_rank, null);

        initView(rootView);

        return rootView;
    }
    public void initView(View rootView){
        focusImage= ((ImageView) rootView.findViewById(R.id.focus_image));
        focusImage.setFocusable(true);
        focusImage.setFocusableInTouchMode(true);
        focusImage.requestFocus();
        programListView= ((ListView) rootView.findViewById(R.id.rank_program_list_view));
        anchorListView=((ListView) rootView.findViewById(R.id.rank_anchor_list_view));
        programlist=new ArrayList<>();
        anchorlist=new ArrayList<>();
        programAdapter =new DiscoverRankAdapter(getContext(),programlist);
        anchorAdapter =new DiscoverRankAdapter(getContext(),anchorlist);
        initData();
        programListView.setAdapter(programAdapter);
        programListView.setOnItemClickListener(this);
        anchorListView.setAdapter(anchorAdapter);
        anchorListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(getActivity(), DiscoverRankAnchorActivity.class);
                intent.putExtra("key",anchorlist.get(i).getKey());
                intent.putExtra("contentType",anchorlist.get(i).getContentType());
                intent.putExtra("title",anchorlist.get(i).getTitle());
                startActivity(intent);
            }
        });
        //focusImage.setScaleType(ImageView.ScaleType.FIT_XY);
        focusImage.setOnClickListener(this);
    }
    public void initData(){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(Urlconfig.RANK_URL_PATH)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        DiscoverRankFirstService service=retrofit.create(DiscoverRankFirstService.class);
        service.datas().enqueue(this);
    }
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (i){
            case 0:
                toDiscoverRankActivity(0);
                break;
            case 1:
                toDiscoverRankActivity(1);
                break;
            case 2:
                toDiscoverRankSecondSingleActivity(2);
                break;
            case 3:
                toDiscoverRankSecondSingleActivity(3);
                break;
            case 4:
                toDiscoverRankSecondSingleActivity(4);
                break;
            case 5:
                toDiscoverRankSecondSingleActivity(5);
                break;
            case 6:
                toDiscoverRankSecondSingleActivity(6);
                break;
            case 7:
                toDiscoverRankSecondSingleActivity(7);
                break;
            case 8:
                toDiscoverRankSecondSingleActivity(8);
                break;
            case 9:
                toDiscoverRankSecondSingleActivity(9);
                break;
            case 10:
                toDiscoverRankSecondSingleActivity(10);
                break;
            case 11:
                toDiscoverRankSecondSingleActivity(11);
                break;
            case 12:
                toDiscoverRankSecondSingleActivity(12);
                break;
            case 13:
                toDiscoverRankSecondSingleActivity(13);
                break;
            case 14:
                toDiscoverRankActivity(14);
                break;
             case 15:
                 toDiscoverRankSecondSingleActivity(15);
                break;

        }

    }
    public void toDiscoverRankActivity(int i){
        Intent intent=new Intent(getActivity(), DiscoverRankActivity.class);
        intent.putExtra("key",programlist.get(i).getKey());
        intent.putExtra("contentType",programlist.get(i).getContentType());
        intent.putExtra("title",programlist.get(i).getTitle());
        startActivity(intent);
    }
    public void toDiscoverRankSecondSingleActivity(int i){
        Intent intent=new Intent(getActivity(), DiscoverRankSecondSingleActivity.class);
        intent.putExtra("key",programlist.get(i).getKey());
        intent.putExtra("contentType",programlist.get(i).getContentType());
        intent.putExtra("title",programlist.get(i).getTitle());
        startActivity(intent);
    }
    @Override
    public void onResponse(Call<DiscoverRankEntity> call, Response<DiscoverRankEntity> response) {
        List<DiscoverRankDataEntity> datas=response.body().getDatas();
        focusEnity=response.body().getFocusImages();
        Picasso.with(getContext())
                .load(focusEnity.getList().get(0).getPic())
                //.transform(transformation)
                .into(focusImage);
        DiscoverRankDataEntity dataEntity=datas.get(0);
        DiscoverRankDataEntity dataEntity1=datas.get(1);
        programlist.addAll(dataEntity.getList());
        anchorlist.addAll(dataEntity1.getList());
        programAdapter.notifyDataSetChanged();
        anchorAdapter.notifyDataSetChanged();

    }

    @Override
    public void onFailure(Call<DiscoverRankEntity> call, Throwable t) {

    }

    @Override
    public void onClick(View view) {
        Intent intent=new Intent(getActivity(), DiscoverWebActivity.class);
        intent.putExtra("url",focusEnity.getList().get(0).getUrl());
        startActivity(intent);
    }
    Transformation transformation = new Transformation() {

        @Override
        public Bitmap transform(Bitmap source) {

            int targetWidth = focusImage.getWidth();
            //LogCat.i("source.getHeight()="+source.getHeight()+",source.getWidth()="+source.getWidth()+",targetWidth="+targetWidth);

            if(source.getWidth()==0){
                return source;
            }

            //如果图片小于设置的宽度，则返回原图
            if(source.getWidth()<targetWidth){
                return source;
            }else{
                //如果图片大小大于等于设置的宽度，则按照设置的宽度比例来缩放
                double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                int targetHeight = (int) (targetWidth * aspectRatio);
                if (targetHeight != 0 && targetWidth != 0) {
                    Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                    if (result != source) {
                        // Same bitmap is returned if sizes are the same
                        source.recycle();
                    }
                    return result;
                } else {
                    return source;
                }
            }

        }

        @Override
        public String key() {
            return "transformation" + " desiredWidth";
        }
    };
}
